//
//  ViewController.m
//  AlinkDemo
//
//  Created by Dong on 2016/12/28.
//  Copyright © 2016年 aliyun. All rights reserved.
//

#import "ViewController.h"
#import "BLEDiscoverController.h"

#import <AKDevKit/AKOpenDevKit.h>
#import <AlinkSDK/AlinkOpenSDK.h>
#import <BoneKit/BoneKit.h>

#import <ALBBOpenAccountSDK/ALBBOpenAccountSDK.h>
#import <UTDID/UTDevice.h>

#import <AKDebugDashboard/AKDebugDashboard.h>

#import <CocoaLumberjack/CocoaLumberjack.h>
#import "LBXScanPermissions.h"
#import "LBXScanViewStyle.h"
#import "LBXScanViewController.h"
#import "StyleDIY.h"
#import "AppDelegate.h"

#import "t11Config.h"
#import "Update.h"
#import "Constant.h"

#import  <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

static NSString *const kRNTestAlertMessage = @"请输入ip地址";

extern DDLogLevel ddLogLevel;

@interface ViewController ()

@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) NSMutableArray *data;
//callCenter此参数必须为成员变量，要不然在ARC编译环境下，在这个函数之后就会被销毁，无法正常监听来电状态！
@property(nonatomic,strong)CTCallCenter *callCenter;

@end

@implementation ViewController
- (void)labelTouchUpInside:(UITapGestureRecognizer *)recognizer {

//    UILabel *label=(UILabel*)recognizer.view;
//
//    NSLog(@"%@被点击了",label.text);

    NSLog(@"--------点击事件");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
#ifdef team11Debug
    UIView *dog = [[UINib nibWithNibName:@"T11OfflineView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    [self.view addSubview:dog];
//    UIButton *btn = [dog viewWithTag:110];
//    NSLog(@"----> btn = %@",btn);
//    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
    dog.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];

    [dog addGestureRecognizer:labelTapGestureRecognizer];


    self.table = [[UITableView alloc] initWithFrame:self.view.frame];
    self.table.dataSource = self;
    self.table.delegate = self;
    [self.view addSubview:self.table];

    [self createHeader];
    self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
#else
    NSLog(@"准备开始启动啦~~~");
    [self startTeam11BonePage];
#endif
    
    if ([[AlinkAccount sharedInstance] isLogin]) {
        [self bindMsgChannel];

    }
    
    //注册打开扫描仪通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startScan) name:@"OpenScan" object:nil];
    //注册二维码扫描回调
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(qrCodeResult:) name:@"qrcodeResult" object:nil];
    //注册跳转猫眼
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(toEyes) name:@"toEyes" object:nil];
    //注册绑定消息通道
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(bindMsgChannel) name:@"bindMsgChannel" object:nil];
    //注册解绑消息通道
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(unbindMsgChannel) name:@"unbindMsgChannel" object:nil];
    //注册登录怡康
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginYK) name:@"loginYK" object:nil];
    
}
-(void)dealloc{
    //页面销毁清除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
/**************监听手机来电start****************/
- (void)watchPhone{
    self.callCenter = [[CTCallCenter alloc] init];
    
    self.callCenter.callEventHandler = ^(CTCall* call) {
        
        if ([call.callState isEqualToString:CTCallStateDisconnected])
            
        {
            
            NSLog(@"Call has been disconnected");
            
        }
        
        else if ([call.callState isEqualToString:CTCallStateConnected])
            
        {
            
            NSLog(@"Call has just been connected");
            
        }
        
        else if([call.callState isEqualToString:CTCallStateIncoming])
            
        {
            
            NSLog(@"Call is incoming");
            //挂电话
            [[NSNotificationCenter defaultCenter]postNotificationName:@"fireCall" object:nil];
            
        }
        
        else if ([call.callState isEqualToString:CTCallStateDialing])
            
        {
            
            NSLog(@"call is dialing");
            
        }
        
        else
            
        {
            
            NSLog(@"Nothing is done");
            
        }
        
    };
    
}
/**************监听手机来电end****************/
/**************登录怡康服务start****************/
-(void)loginYK{
    if(![[AlinkAccount sharedInstance] isLogin])return;
    NSDictionary *session =  [[AlinkAccount sharedInstance] currentSession];
    NSString *userName = session[@"ak_account"];
    if (userName) {
        [YKBusinessFramework equesSdkLoginWithUrl:SERVERADDRESS username:userName appKey:APPKEY];
    }
}
/**************登录怡康服务end****************/
/**************二维码扫返回值start****************/
-(void)qrCodeResult:(NSNotification *)notification{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"BarcodeScanSuccessForRN" object:notification.object];
}
/**************二维码扫描返回值end****************/

/**************消息通道start****************/
//绑定
-(void)bindMsgChannel{
    [[AlinkSDK sharedManager] invokeWithMethod:@"mtop.openalink.user.channel.bind" params:@{@"type":@"AGOO",@"deviceId":[UTDevice utdid]} completionHandler:^(AlinkResponse *response) {
        if (response.successed) {
            NSLog(@"push 绑定成功___________ %@",response);
        }
        else {
            NSLog(@"push 绑定失败 %@",response.responseError);
        }
    }];
}
//解绑
-(void)unbindMsgChannel{
    [[AlinkSDK sharedManager] invokeWithMethod:@"mtop.openalink.user.channel.unbind" params:@{@"type":@"AGOO",@"deviceId":[UTDevice utdid]} completionHandler:^(AlinkResponse *response) {
        if (response.successed) {
            NSLog(@"push 解除绑定成功");
            [[AlinkAccount sharedInstance] logout:^(NSError * _Nonnull error) {  }];
        }
        else {
            NSLog(@"push 解除绑定失败 %@",response.responseError);
            
        }
    }];
}
/**************消息通道end****************/
/*********版本更新start**********/
-(void)update{
    double delayInSeconds = 3.0;
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);   
    
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //执行事件
        [self timerHandle];
    });
}
-(void)timerHandle{
    [[[Update alloc]init] exec:self];
}
/*********版本更新end**********/
/*********启动bonestart**********/
- (void)startTeam11BonePage {
    //    NSString *boneUrl = @"p.aliplus.com/sdsdemo.api";
    //    NSString *boneUrl = @"p.aliplus.com/TEAM11_SECURITY_GATEWAY_T11SMARTLOCK.home";
    
#ifdef dayiBoxTestBoneURL
    NSString *boneUrl = @"p.aliplus.com/DAYIKEJI_SECURITY_SAFE_T11SMARTSTRONGBOX.home";
#else
    NSString *boneUrl = @"p.aliplus.com/DAYIKEJI_SECURITY_GATEWAY_SMARTLOCK.home";
#endif
    
    NSURL *url = [NSURL URLWithString:boneUrl];
    NSLog(@"%s:%d url = %@", __func__, __LINE__, url);
    [[BoneRouter defaultRouter] open:url completionHandler:^(BOOL success) {
        NSLog(@"open:%@ %d-----------------------------------------------------------", url, success);
        if (!success) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法打开指定URL1" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            
        }
    }];
}
/*********启动bonend**********/
/*********启动二维码扫描start**********/

- (void)startScan {
    LBXScanViewController *vc = [LBXScanViewController new];
    vc.style = [StyleDIY ZhiFuBaoStyle];
    vc.isOpenInterestRect = YES;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:vc animated:YES];
   
}
/*********启动二维码扫描end**********/
/*********去猫眼start**********/
- (void)toEyes{
    
    NSDictionary *session =  [[AlinkAccount sharedInstance] currentSession];
    if (session) {
        self.navigationController.navigationBarHidden = NO;
        [self performSegueWithIdentifier:@"Eyes" sender:nil];
        
    }else{
        [SVProgressHUD showErrorWithStatus:@"请登录"];
    }
    
}
/*********去猫眼end**********/
- (void)fetchData {

    NSURLSession *session = [NSURLSession sharedSession];
    __weak __typeof(self) weakSelf = self;

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gaic.alicdn.com/tms/boneDemoConfig.json"]] completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {

        if (!error) {

            NSError *err;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&err];
            NSLog(@"tmsResult: %@", dic);
            //deviceConfigURL  title = "Bone"; url = "";
            if ([[dic objectForKey:@"boneAppList"] isKindOfClass:[NSArray class]]) {
                weakSelf.data = [dic objectForKey:@"boneAppList"];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.table reloadData];
                });

            }
        } else {


        }

    }];


    [dataTask resume];


}


- (void)createHeader {


    NSArray *buttons = [[NSArray alloc] initWithObjects:@"登录",
                                                        @"登出", @"蓝牙发现",
                                                        @"设备配网", @"扫一扫",
                                                        @"BoneKit测试", @"调试日志", @"BoneRouter", @"十一队Bone",@"猫眼",
                        
                    nil];


    int btnWidth = self.view.frame.size.width / 3;
    int btnHeight = 40;
    int topMargin = 88;

    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, topMargin + 20 + (btnHeight + 10) * ceil([buttons count] / 2.0))];
    UIColor *c1 = [UIColor colorWithRed:0.0 / 256 green:199.0 / 255 blue:178.0 / 255 alpha:1];
    for (int i = 0; i < [buttons count]; i++) {
        UIButton *btn;
        int f = i % 2;
        int top = 25;

        if (f == 0) {
            btn = [[UIButton alloc] initWithFrame:CGRectMake(20, topMargin + top * i, btnWidth, btnHeight)];
        }

        if (f == 1) {
            btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 20 - btnWidth, topMargin + top * (i - 1), btnWidth, btnHeight)];
        }


        btn.tag = i;
        [btn setTitle:[buttons objectAtIndex:i] forState:UIControlStateNormal];
        // 将UIControlEventTouchDown事件绑定到click函数上
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];

        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:3.0];
        [btn.layer setBorderWidth:1];
        btn.layer.borderColor = c1.CGColor;
        [btn setTitleColor:c1 forState:UIControlStateNormal];

        [header addSubview:btn];
    }

    self.table.tableHeaderView = header;
    self.table.tableFooterView = [[UIView alloc] init];

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 160) / 2, 5, 78, 76)];
    img.image = [UIImage imageNamed:@"alismart"];
    [header addSubview:img];


    UILabel *info = [[UILabel alloc] initWithFrame:CGRectMake(img.frame.origin.x + img.frame.size.width + 8, 17, 120, 44)];
    info.text = @"IoT 开发工具\nDemoApp";
    info.numberOfLines = 2;
    [header addSubview:info];
    info.textColor = c1;
    info.font = [UIFont boldSystemFontOfSize:14];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //复用标志
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

    }

    NSDictionary *dict = self.data[indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"title"] ?: @"";
    UIColor *c2 = [UIColor colorWithRed:153.0 / 256 green:153.0 / 255 blue:153.0 / 255 alpha:1];
    cell.textLabel.textColor = c2;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    return cell;
}

/**
 * 处理下面的json数据给出来的tableView的handler事件
 * @param tableView
 * @param indexPath
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *dict = self.data[indexPath.row];
    NSURL *url = [NSURL URLWithString:[@"alink://" stringByAppendingString:[dict objectForKey:@"url"] ?: @""]];
    NSLog(@"%s:%d url = %s", __func__, __LINE__, url);
    [[BoneRouter defaultRouter] open:url completionHandler:^(BOOL success) {
        NSLog(@"open:%@ %d", url, success);
        if (!success) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法打开指定URL" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.isEyesOpen = NO;
//    [super viewWillDisappear:animated];
//
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
//
//    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//
    [self.navigationController setNavigationBarHidden:YES animated:NO];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//    [super viewWillAppear:animated];
    [self fetchData];
}

-(void)viewDidAppear:(BOOL)animated{
    [self update];
}

- (void)click:(UIButton *)button {
    NSLog(@"-----------button clicked !!!!!!!%ld", button.tag);
    switch (button.tag) {
        case 0: {
            [self startLogin];
        }

            break;
        case 1: {
            [self startLogOut:button];
        }
            break;

        case 2: {
            //蓝牙入口
            BLEDiscoverController *vc = [[BLEDiscoverController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3: {
            [self startConfigure];
        }
            break;
        case 4: {

            [self startScan];
        }
            break;
        case 5: {
            [self onDebugReactNative];
        }
            break;
        case 6: {
            [self openLog];
        }
            break;
        case 7: {
            NSURL *url = [NSURL URLWithString:@"https://www.aliyun.com"];
            [[BoneRouter defaultRouter] open:url completionHandler:^(BOOL success) {
                NSLog(@"open:%@ %d", url, success);
                if (!success) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法打开指定URL" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
                }
            }];
        }
            break;
        case 8: {
            DDLogInfo(@"startTeam11BonePage button clicked!");
            [self startTeam11BonePage];
            break;

        }
        case 9: {
            [self toEyes];
        }
            break;
        case 110: {
            DDLogInfo(@"startTeam11BonePage button clicked!");
            [self startTeam11BonePage];
        }
            break;
        default:
            break;
    }
}

typedef void(^AKRouterCompletionHandler)(NSError *error, NSDictionary *info);

- (void)startConfigure {
    BoneRouterCompletionHandler handler = ^(NSError *error, NSDictionary *info) {
        NSString *str = [NSString stringWithFormat:@"配网成功：%@", info];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str ?: @"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    };

    NSDictionary *options = @{BoneRouterCompletionHandlerKey: handler};
    [[BoneRouter defaultRouter] openCode:@"deviceConfig" options:options completionHandler:^(BOOL success) {
        if (!success) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法打开指定URL" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }];
}


//- (void)startTeam11BonePage {
////    NSString *boneUrl = @"p.aliplus.com/sdsdemo.api";
//    NSString *boneUrl = @"alink://p.aliplus.com/TEAM11_SECURITY_GATEWAY_T11SMARTLOCK.home";
//    NSURL *url = [NSURL URLWithString:boneUrl];
//    NSLog(@"%s:%d url = %@", __func__, __LINE__, url);
//    [[BoneRouter defaultRouter] open:url completionHandler:^(BOOL success) {
//        NSLog(@"open:%@ %d", url, success);
//        if (!success) {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法打开指定URL" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//            [alert show];
//        }
//    }];
//}


//- (void)startScan {
//    LBXScanViewController *vc = [LBXScanViewController new];
//    vc.style = [StyleDIY ZhiFuBaoStyle];
//    vc.isOpenInterestRect = YES;
//    self.navigationController.navigationBarHidden = NO;
//    [self.navigationController pushViewController:vc animated:YES];
//
//
//
//}

- (void)openLog {
    [AKDebugDashboard sharedDashboard].fullScreen = YES;
}

- (void)alertWithTitle:(NSString *)title msg:(NSString *)msg {
    UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title ?: @"" message:msg ?: @"" preferredStyle:UIAlertControllerStyleAlert];
    [alertViewController addAction:[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {

    }]];

    [self presentViewController:alertViewController animated:YES completion:^{

    }];

}

- (void)startLogin {
//    if ([[AlinkAccount sharedInstance] isLogin]) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经登录过" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//        [alert show];
//    } else {
        [[AlinkAccount sharedInstance] loginWithViewController:nil completionHandler:^(BOOL isSuccessful, NSDictionary *loginResult) {
            DDLogInfo(@"Alink login success: %@, result: %@", @(isSuccessful), loginResult);

            [kAlinkSDK invokeWithMethod:@"mtop.openalink.user.channel.bind" params:@{@"type": @"AGOO", @"deviceId": [UTDevice utdid]} completionHandler:^(AlinkResponse *response) {

                if (response.successed) {
                    DDLogInfo(@"push 绑定成功");
                } else {
                    DDLogInfo(@"push 绑定失败 %@", response.responseError);
                }
            }];
        }                                   cancelationHandler:^{
            DDLogInfo(@"Alink login canceled!");
        }];

//    }
}

- (void)startLogOut:(UIButton *)btn {
    __weak typeof(self) weakSelf = self;
    if ([[AlinkAccount sharedInstance] isLogin]) {
        //防止多次点击
        btn.enabled = NO;
        // 调用 logout 之前必须先调用mtop.openalink.user.channel.unbind
        [kAlinkSDK invokeWithMethod:@"mtop.openalink.user.channel.unbind" params:@{@"type": @"AGOO", @"deviceId": [UTDevice utdid]} completionHandler:^(AlinkResponse *response) {
            if (response.successed) {
                DDLogInfo(@"push 解除绑定成功");
                [[AlinkAccount sharedInstance] logout:^(NSError *_Nonnull error) {
                    btn.enabled = YES;
                    DDLogInfo(@"logout --err %@", error);
                    NSString *msg = error ? [NSString stringWithFormat:@"登出失败：%@", error] : @"登出成功";
                    [weakSelf alertWithTitle:@"提示" msg:msg];
                }];
            } else {
                btn.enabled = YES;
                DDLogInfo(@"push 解除绑定 --err %@", response.responseError);
                NSString *msg = [NSString stringWithFormat:@"AGOO解绑失败：%@", response.responseError];
                [weakSelf alertWithTitle:@"提示" msg:msg];
            }
        }];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经处于登出状态" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)onDebugReactNative {
    __weak typeof(self) wSelf = self;

    NSString *boneDebugIPCache = [[NSUserDefaults standardUserDefaults] objectForKey:@"boneDebugIP"];

    NSString *msg = kRNTestAlertMessage;

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:msg
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
        textField.placeholder = @"ip address";
        textField.text = boneDebugIPCache ?: @"";
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        NSString *host = alertController.textFields.firstObject.text;
        NSString *trimmedHost = [host stringByTrimmingCharactersInSet:
                [NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if (trimmedHost.length > 0) {
            
            [wSelf requestRNLinkWithHost:trimmedHost];
            [[NSUserDefaults standardUserDefaults] setObject:trimmedHost forKey:@"boneDebugIP"];
            
        }
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
    }]];
    [self presentViewController:alertController animated:true completion:^{

    }];
}

- (void)requestRNLinkWithHost:(NSString *)theHost {
    NSString *link = [NSString stringWithFormat:@"http://%@:8081/boneDebugUrl?platform=ios&ip=%@", theHost, theHost];
    NSURL *url = [NSURL URLWithString:link];
    __weak typeof(self) wSelf = self;

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {

        if (error) {
            [wSelf showErrorAlert:error];
            return;
        }
        NSError *parseError = nil;
        NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data
                                                                       options:NSJSONReadingAllowFragments
                                                                         error:&parseError];
        if (parseError) {
            [wSelf showErrorAlert:parseError];
            return;
        }

        int code = [[responseObject objectForKey:@"code"] intValue];
        if (code == 200) {
            NSDictionary *dataObject = [responseObject objectForKey:@"data"];
            NSString *urlString = dataObject[@"url"];
            urlString = [urlString stringByReplacingOccurrencesOfString:@"http:" withString:@"react:"];
            dispatch_async(dispatch_get_main_queue(), ^{

                BoneRouterCompletionHandler handler = ^(NSError *error, NSDictionary *info) {
                    NSLog(@"%s error:%@ info:%@", __FUNCTION__, error, info);
                };

                NSMutableDictionary *options = [dataObject mutableCopy];
                options[BoneRouterCompletionHandlerKey] = handler;

                [[BoneRouter defaultRouter] open:[NSURL URLWithString:urlString] options:options completionHandler:^(BOOL success) {

                }];
            });
        } else {
            NSLog(@"error: %@", error.description);
        }
    }];
    [dataTask resume];
}

- (void)showErrorAlert:(NSError *)error {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:[error localizedDescription]
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
    }]];

    [self presentViewController:alertController animated:true completion:^{

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
